<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="日志类型" prop="type" >
                <el-input v-model="queryParams.type" placeholder="请输入" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="日志简要" prop="title" >
                <el-input v-model="queryParams.title" placeholder="请输入" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="IP过滤" prop="ip" >
                <el-input v-model="queryParams.ip" placeholder="请输入" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="域名过滤" prop="domain" >
                <el-input v-model="queryParams.domain" placeholder="请输入" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="记录时间" >
                <el-date-picker v-model="dateRange" value-format='yyyy-MM-dd HH:mm:ss' type="datetimerange" :picker-options="pickerOptions" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"><span style="color: white; font-weight: bold; ">搜索</span></el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button type="danger" plain size="mini" @click="handleDeleteAll">清空日志</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList">
            <el-table-column label="#" align="center" prop="id" min-width="10%" />
            <el-table-column label="日志类型" align="center" prop="type" min-width="10%" />
            <el-table-column label="日志简要" align="center" prop="title" min-width="20%" />
            <el-table-column label="日志内容" align="center" prop="user_ip" min-width="40%">
                <template slot-scope="scope">
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>ip</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.ip }}</el-col>
                    </el-row>
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>ua</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.ua }}</el-col>
                    </el-row>
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>host</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.host }}</el-col>
                    </el-row>
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>info</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.info }}</el-col>
                    </el-row>
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>domain</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.domain }}</el-col>
                    </el-row>
                    <el-row :gutter="10" class="mb8 table-row">
                        <el-col :span="3"><el-tag>remark</el-tag></el-col>
                        <el-col :span="21">{{ scope.row.remark }}</el-col>
                    </el-row>
                </template>
            </el-table-column>
            <el-table-column label="记录时间" align="center" prop="create_time" min-width="20%" />
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.per_page" @pagination="getList" />
    </div>
</template>

<script>

import { queryLogList, deleteAll } from "@/api/logs/visitor";
export default {
    components: {
    },
    name: "visitorList",
    dicts: ["sys_normal_disable"],
    data() {
        return {
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表单参数
            form: {},
            // 日期范围
            dateRange: [],
            // 遮罩层
            loading: false,
            // 查询参数
            queryParams: {
                page: 1,
                per_page: 10,
                type: undefined,
                title: undefined,
                ip: undefined,
                domain: undefined,
                start_time: undefined,
                end_time: undefined
            },
            dataList: [],
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [start, end]);
                    }
                }, {
                    text: '最近三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [start, end]);
                    }
                }]
            },
        };
    },
    created() {
        this.getList();
    },
    methods: {
        handleQuery() {
            this.queryParams.page = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleDelete() { },
        getList() {
            //   this.loading = true;
            queryLogList(
                this.addDateRange2(this.queryParams, this.dateRange)
            ).then((response) => {
                this.dataList = response.results;
                this.total = response.total;
                //   this.loading = false;
            });
        },
        handleDeleteAll() {
            this.$confirm('是否确认删除全部?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteAll().then((response) => {
                    this.getList()
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                });
            })
        },
    },
};
</script>

<style lang="scss" scoped>
.table-row {
    text-align: left;
}
</style>